<!--
 * @Descripttion: 批量下单管理
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2021-03-11 15:42:01
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-05-26 17:23:32
-->

<template>
  <div class="lb-custom">
    <top-nav />
    <div class="page-main">
      <el-row class="page-search-form">
        <el-form
          @submit.native.prevent
          :inline="true"
          :model="searchForm.list"
          ref="listForm"
        >
          <el-form-item label="组局标题" prop="title">
            <el-input v-model="searchForm.list.title" placeholder="请输入组局标题"></el-input>
          </el-form-item>
          <!-- <el-form-item label="组局分类id" prop="class_id">
            <el-input v-model="searchForm.list.class_id" placeholder="请输入组局标题"></el-input>
          </el-form-item> -->
          <el-form-item label="加入时间" prop="start_time">
            <el-date-picker
              @change="getTableDataList(1, 'list')"
              v-model="searchForm.start_time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="timestamp"
              :picker-options="pickerOptions"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <lb-button
              size="medium"
              type="primary"
              icon="el-icon-search"
              style="margin-right: 5px"
              @click="getTableDataList(1, 'list')"
              >{{ $t('action.search') }}</lb-button
            >
            <lb-button
              size="medium"
              icon="el-icon-refresh-left"
              style="margin-right: 5px"
              @click="resetForm('list')"
              >{{ $t('action.reset') }}</lb-button
            >
          </el-form-item>
        </el-form>
      </el-row>
      <el-table
        v-loading="loading.list"
        :data="tableData"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        style="width: 100%"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <p style="font-size: 15px;margin: 20px 0;">>报名列表</p>
            <el-table :data="props.row.pay_user_list" style="width: 50%">
              <el-table-column prop="create_time" label="报名时间" width="200">
                <template slot-scope="scope2">
                  <span>{{ scope2.row.create_time | handleTime(1) }}</span>
                  <span>{{ scope2.row.create_time | handleTime(2) }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="nickName" label="微信昵称" width="100"></el-table-column>
              <el-table-column label="微信头像">
                <template slot-scope="scope2">
                  <lb-image :src="scope2.row.avatarUrl" />
                </template>
              </el-table-column>
              <el-table-column prop="write_code" label="签到码" width="100"></el-table-column>
              <el-table-column prop="write_time" label="是否已签到">
                <template slot-scope="scope2">
                  <span v-if="scope2.row.write_time>0" style="color: green;">已签到</span>
                  <span v-else style="color: red;">未签到</span>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="phone" label="发起人微信昵称">
          <template slot-scope="scope">
            <span>{{ scope.row.faqi_user_info.nickName }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="发起人头像">
          <template slot-scope="scope">
            <lb-image :src="scope.row.faqi_user_info.avatarUrl" />

          </template>
        </el-table-column>
        <el-table-column prop="phone" label="发起人手机号">
          <template slot-scope="scope">
            <span>{{ scope.row.faqi_user_info.phone }}</span>
          </template>
        </el-table-column>
        <el-table-column label="组局金额" prop="money"></el-table-column>
        <el-table-column label="组局标题" prop="title">
          
        </el-table-column>
        <el-table-column prop="pay_count" label="报名人数">
        </el-table-column>
        <el-table-column prop="class_id" label="组局分类">
          <template slot-scope="scope">
            <span>{{ scope.row.class_info.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="city" label="组局城市">
          <template slot-scope="scope">
            <span>{{ scope.row.city_info.title }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="map_name" label="组局地点">
        </el-table-column>
        <el-table-column prop="map_jd" label="组局经度">
        </el-table-column>
        <el-table-column prop="map_wd" label="组局纬度">
        </el-table-column>
        <el-table-column prop="count" label="组局人数">
        </el-table-column>
        <el-table-column prop="count" label="报名开始时间">
          <template slot-scope="scope">
            
            <span>{{ scope.row.application_begin_time | handleTime(1) }}</span>
            <span>{{ scope.row.application_begin_time | handleTime(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="count" label="报名结束时间">
          <template slot-scope="scope">
            
            <span>{{ scope.row.application_over_time | handleTime(1) }}</span>
            <span>{{ scope.row.application_over_time | handleTime(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="count" label="活动时间">
          <template slot-scope="scope">
            <span>{{ scope.row.salon_time | handleTime(1) }}</span>
            <span>{{ scope.row.salon_time | handleTime(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="salon_date" label="活动具体时间段"></el-table-column>
        <el-table-column prop="content" label="活动描述"></el-table-column>

        <el-table-column prop="create_time" label="创建时间" min-width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.create_time | handleTime(1) }}</span>
            <span>{{ scope.row.create_time | handleTime(2) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" fixed="right">
          <template slot-scope="scope">

            <el-tag v-if="scope.row.status == 1" type="warning">审核中</el-tag>
            <el-tag v-if="scope.row.status == 2" type="success">审核通过</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 1"class="table-operate">
              <lb-button
                size="mini"
                plain
                type="success"
                @click="toSH(scope.row)"
                >审核</lb-button
              >
            </div>
            <div class="table-operate">
              <lb-button
                size="mini"
                plain
                type="danger"
                @click="toDels(scope.row)"
                >删除</lb-button
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.list.page"
        :pageSize="searchForm.list.limit"
        :total="total"
        @handleSizeChange="handleSizeChange($event, 'list')"
        @handleCurrentChange="handleCurrentChange($event, 'list')"
      >
      </lb-page>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import moment from 'moment'
export default {
  data () {
    let checkPrice = (rule, value, callback) => {
      let reg = /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
      if (this.payForm.type === 2 && !reg.test(value)) {
        callback(new Error(`请输入充值金额，最多保留2位小数`))
      } else {
        callback()
      }
    }
    return {
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() > (moment(moment(Date.now()).format('YYYY-MM-DD')).unix() + 24 * 3600 - 1) * 1000
        }
      },
      statusOptions: [
        {
          label: '全部',
          value: 0
        },
        {
          // label: '已授权手机号',
          label: '未授权微信昵称/头像',
          value: 1
        },
        {
          label: '已授权微信昵称/头像',
          value: 2
        }
      ],
      pagePermission: [],
      loading: false,
      searchForm: {
        list: {
          page: 1,
          limit: 10,
          start_time: '',
          end_time: '',
          title: '',
          class_id: ''
        },
        pay: {
          page: 1,
          limit: 10,
          nickName: '',
          status: 1
        }
      },
      tableData: [],
      total: { list: 0, pay: 0 },
      showDialog: { pay: false },
      payForm: {
        id: '',
        nickName: '',
        avatarUrl: '',
        phone: '',
        type: 1,
        card_id: 0,
        price: ''
      },
      payFormRules: {
        type: { required: true, type: 'number', message: '请选择充值方式', trigger: 'blur' },
        price: { required: true, validator: checkPrice, key: 'short_sign', text: '签名', trigger: 'blur' },
      },
      currentRow: {}
    }
  },
  async created () {
    this.routesItem.routes.map(item => {
      if (item.path === '/custom') {
        item.children.map(aitem => {
          if (aitem.name === 'CustomList') {
            this.pagePermission = aitem.meta.pagePermission[0].auth
          }
        })
      }
    })
    await this.getTableDataList(1, 'list')
  },
  computed: {
    ...mapState({
      routesItem: state => state.routes
    })
  },
  methods: {
    async toSH(item) {
      let data = await this.$api.bureau.getBureauOK({
        id: item.id
      })
      if(data.data == '活动审核通过'){
        this.$message({
          message: '活动审核通过',
          type: 'success'
        });
      }
      await this.getTableDataList('', 'list')
    },
    async toDels(item) {
      console.log(item)
      let data = await this.$api.bureau.getBureauDels({
        ids:`[${item.id}]`
      })
      if(data.data == '操作成功'){
        this.$message({
          message: '操作成功',
          type: 'success'
        });
      }
      await this.getTableDataList('', 'list')

      console.log(data)
    },
    resetForm (form) {
      let name = `${form}Form`
      this.$refs[name].resetFields()
      this.getTableDataList(1, form)
    },
    handleSizeChange (val, key) {
      this.searchForm.list.limit = val
      this.handleCurrentChange(1, key)
    },
    handleCurrentChange (val, key) {
      this.searchForm.list.page = val
      this.getTableDataList('', key)
    },
    /**
     * @method 获取列表
     */
    async getTableDataList (flag, key) {
      if (flag) this.searchForm.list.page = 1
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm[key]))
      if (key === 'list') {
        let { start_time: time } = searchForm
        if (time && time.length > 0) {
          searchForm.start_time = time[0] / 1000
          searchForm.end_time = time[1] / 1000
        } else {
          searchForm.start_time = ''
          searchForm.end_time = ''
        }
      }
      let { data } = await this.$api.bureau.getBureauList(searchForm)
      console.log(data)
      this.loading = false
      // if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    },
    confirmDel (id, status) {
      this.$confirm(this.$t('tips.confirmDelete'), this.$t('tips.reminder'), {
        confirmButtonText: this.$t('action.comfirm'),
        cancelButtonText: this.$t('action.cancel'),
        type: 'warning'
      }).then(() => {
        this.updateItem(id, status)
      }).catch(() => {

      })
    },
    async updateItem (id, status) {
      this.$api.storeStatusUpdate({ id, status }).then(res => {
        if (res.code === 200) {
          this.$message.success(this.$t(status === -1 ? 'tips.successDel' : 'tips.successOper'))
          if (status !== -1) return
          this.searchForm.list.page = this.searchForm.list.page < Math.ceil((this.total.list - 1) / this.searchForm.list.limit) ? this.searchForm.list.page : Math.ceil((this.total.list - 1) / this.searchForm.list.limit)
          this.getTableDataList('', 'list')
        } else {
          if (status === -1) return
          this.getTableDataList('', 'list')
        }
      })
    },
    async toDelLabel (uid, id) {
      let { code } = await this.$api.custom.delUserLabel({ user_id: uid, label_id: id })
      if (code !== 200) return
      this.$message.success(this.$t('tips.successDel'))
      this.getTableDataList('', 'list')
    },
    async toShowDialog (key, item = {}) {
      item = JSON.parse(JSON.stringify(item))
      if (key === 'pay') {
        item.type = 1
        item.card_id = 0
        item.price = ''
      }
      for (let i in this[`${key}Form`]) {
        this[`${key}Form`][i] = item[i]
      }
      this.searchForm.pay.title = ''
      await this.getTableDataList(1, key)
      this.showDialog[key] = !this.showDialog[key]
    },
    handleSelectionChange (val) {
      this.currentRow = val
    },

    async submitForm (key) {
      let flag = true
      let { id, card_id: cid, type, price } = this.payForm
      if (type === 2) {
        this.$refs['payForm'].validate((valid) => {
          if (!valid) flag = false
        })
      }
      if (flag) {
        let subForm = { user_id: [id], card_id: cid }
        if (type === 1) {
          if (this.currentRow === null || !this.currentRow.id) {
            this.$message.error(`请选择充值套餐`)
            return
          }
          let { id: cardid } = this.currentRow
          subForm.card_id = cardid
        } else {
          subForm.price = price
        }
        let { code } = await this.$api.finance.payBalanceOrder(subForm)
        if (code !== 200) return
        this.$message.success(this.$t('tips.successSub'))
        this.showDialog[key] = false
        this.getTableDataList('', 'list')
      }
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-form {
  .el-image {
    width: 100px;
    height: 100px;
  }
}
</style>
